<template>
	<view class="container">
		<view class="top">
			<view class="locationBox">
				<uni-icons type="location" size="30" color="#333"></uni-icons>
				<text>{{locationCity}}</text>
			</view>
			<view class="swipe-content">
				<swiper indicator-dots autoplay>
					<swiper-item v-for="(item, index) in imgList" :key="index">
						<image :src="item.imgUrl" class="slide-image" mode="aspectFit"></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="content">
				<!-- <view class="currentTip">当前设备</view> -->
				<view class="equipmentInfo">
					<view class="infoLeft">
						<image src="../../static/icon/home_1.png" mode="aspectFit" class="icon"></image>
						<view class="machine-name">{{ pickerData.merchantId }}</view>
						<text class="tip">(当前设备)</text>
					</view>
					<view class="infoRight">
						<view class="uni-list">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker @change="bindPickerChange($event, array)" mode="selector" :value="index"
										:range="array" :range-key="`merchantId`">
										<button class="mini-btn merchant-btn" type="primary" size="mini">切换设备</button>
									</picker>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="userBox">
			<view class="tagBox">

				<text>热门功能</text>
			</view>
			<view class="listBox">
				<view class="listItem" v-for="(item, index) in userList" :key="index" @click="goRouter(index)">
					<view class="itemLeft">
						<image :src="item.src"></image>
					</view>
					<view class="item-size">
						{{ item.label }}
					</view>

				</view>
			</view>
			<!-- 		<view class="bgImage">
				<image src="../../static/icon/homeBg.jpg"></image>
				<view class="textBox">
					<text>号外号外！！钱宝鲲PAY隆重上线!</text>
				</view>
			</view> -->
			<!-- <service></service> -->
		</view>
	</view>
</template>
<script setup>
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive
	} from "vue";
	import service from "./component/index.vue";
	import {
		queryUserDetail,
		queryAthorization,
		shopLists
	} from "@/api/mine";
	import {
		terminalQuery,
		swipeImage,
		wxLocation
	} from "@/api/common.js";
	import {
		useStore
	} from "@/store/index";
	import {
		showToast
	} from "@/utils/toast";
	/** store实例 */
	const store = useStore();
	let array = ref([]);
	let index = ref(0)
	let settleStatus = ref(null)
	let pickerData = ref({})
	let imgList = ref([]);
	let locationCity = ref("定位获取中")
	let userList = ref([{
			label: "实名认证",

			src: '../../static/icon/home_2.png',
		},
		{
			label: "新增商户",

			src: '../../static/icon/home_3.png',
		},
		{
			label: "终端管理",
			src: '../../static/icon/home_4.png',
		},
		{
			label: "银行卡管理",

			src: '../../static/icon/home_5.png',
		},
		{
			label: "信用卡认证",

			src: '../../static/icon/home_6.png',
		},
		{
			label: "磁条卡认证",
			src: '../../static/icon/home_7.png',
		},
		{
			label: "店铺管理",

			src: '../../static/icon/home_8.png',
		},
		{
			label: "额度费率",

			src: '../../static/icon/home_9.png',
		},
		{
			label: "我的交易",
			src: '../../static/icon/home_10.png',
		},
		{
			label: "扫码认证",

			src: '../../static/icon/home_11.png',
		},
		{
			label: "合并到账",

			src: '../../static/icon/home_12.png',
		},
		{
			label: "设置",
			src: '../../static/icon/home_13.png',
		},
	]);
const locationParams=ref({
	lat:"",
	lng:''
})
	let goRouter = (val) => {
		if (val === 0) {
			uni.navigateTo({
				url: "/authentication/realName/index",
			});

		} else if (val === 7) {

			uni.navigateTo({
				url: `/businessRate/rate/index?merchantId=${pickerData.value.merchantId}`,
			});
		} else if (val === 3) {
			//进入银行卡页面前  判斷  有没实名 又沒添加店鋪
			checkAuth("5");

		} else if (val === 1) {
			//添加店铺前判断有没实名

			checkAuth("4");


		} else if (val === 8) {

			uni.navigateTo({
				url: "/pages/orderList/index",
			});
		} else if (val === 11) {

			uni.navigateTo({
				url: "/pagesLogin/setting/index",
			});
		} else if (val === 2) {
			//实名 新增店铺 绑卡
			checkAuth("6");

		} else if (val === 6) {

			uni.navigateTo({
				url: "/pagesDetail/shopList/index",
			});
		} else if (val === 4) {
			checkAuth("2");

		} else if (val === 5) {
			checkAuth("3");
		} else if (val === 10) {
			//进入提现页面前  判斷  有没实名 又沒添加店鋪 又没绑卡
			checkAuth("7");

		} else if (val === 9) {

			showToast("暂未开放");

		}
	}
	onLoad(() => {
		getAddress();
		getEquipmentList();
		getSwipeImage();


	})
	onShow(() => {

		checkAuth("1");

	})

	function getShopList(type) {
		//1 银行卡管理 2绑sn
		shopLists(store.userInfo.userId).then((res) => {
			//未绑定显示unbind和bind
			console.log("res=" + res.sn)
			if (res.sn || res.bind || res.unbind) {
				settleStatus = store.settleStatus;
				console.log("settleStatus=" + settleStatus)
				if (settleStatus == "0") {
					uni.showModal({
						title: '提示',
						content: '您尚未绑定储蓄卡，请先进行绑卡!',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.navigateTo({
									url: "/authentication/addBank/index",
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					if (type == "1") {
						uni.navigateTo({
							url: "/authentication/realBank/index",
						});
					} else if (type == "2") {
						uni.navigateTo({
							url: "/pagesDetail/bindSn/index",
						});
					} else if (type == "3") {
						uni.navigateTo({
							url: "/settle/index/index",
						});
					}

				}

			} else {
				//先去添加店铺
				uni.showModal({
					title: '提示',
					content: '您尚未添加店铺，请先去添加店铺',
					showCancel: true,
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.navigateTo({
								url: "/pagesLogin/merchantRegistration/index",
							});
						} else if (res.cancel) {
							console.log('用户点击取消');

						}
					}
				});
			}
		});
	}

	function checkBindCard() {
		//未绑定银行卡，弹出绑定银行卡
		settleStatus = store.settleStatus;
		console.log("settleStatus=" + settleStatus)
		if (settleStatus == "0") {
			uni.showModal({
				title: '提示',
				content: '您尚未绑定储蓄卡，请先进行绑卡!',
				showCancel: false,
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.navigateTo({
							url: "/authentication/addBank/index",
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	}


	function checkAuth(type) {
		queryUserDetail(store.userInfo.userId).then((res) => {
				console.log("res=" + JSON.stringify(res))
				let authorizationFlag = res.status
				let userInfo = res
				// 存储登录信息
				store.setData({
					userInfo,
				});
				uni.setStorageSync("userInfo", userInfo);
				if (authorizationFlag != 'CHECK_SUCC') {
					uni.showModal({
						title: '提示',
						content: '您尚未通过实名认证，请先进行实名认证!',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.navigateTo({
									url: `/authentication/realName/index?authorizationFlag=${true}`,
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					if (type == "2") {
						uni.navigateTo({
							url: "/authentication/addCredit/index?type=" + type,
						});
					} else if (type == "3") {
						uni.navigateTo({
							url: "/authentication/addCredit/index?type=" + type,
						});
					} else if (type == "4") {
						uni.navigateTo({
							url: "/pagesLogin/merchantRegistration/index",
						});
					} else if (type == "5") {
						getShopList("1");

					} else if (type == "6") {
						getShopList("2");

					} else if (type == "7") {
						getShopList("3");

					}

				}
			})
			.catch((error) => {
				showToast(error.data.message || error.data.error);
			});

	}
	let getSwipeImage = () => {
		const that = this;

		swipeImage({
				params: {
					imageType: "01",
					oemUserNo: 'BU100002'
				},
			}).then((res) => {
				console.log("res=" + JSON.stringify(res))
				imgList.value = res;
			})
			.catch((error) => {
				showToast(error.data.message || error.data.error);
			});

	}
	// 获取当前经纬度
	let getAddress = () => {
		console.log("获取定位")
		uni.getLocation({
			type: 'wgs84',
			success: function(res) {
				console.log('当前位置的经度：' + res.longitude);
				console.log('当前位置的纬度：' + res.latitude);
				// uni.request({
				// 	url: '/amap/ws/geocoder/v1/',
				// 	method: 'GET',
				// 	data: {
				// 		location: res.latitude + ',' + res.longitude,
				// 		key: 'JUJBZ-N3B3J-T6JFN-XGN2S-FDUB3-L7FBP'
				// 	},

				// 	success: function(res) {
				// 		console.log('地址解析成功：', res);
				// 		let data = res.data;
				// 		if (data.status == 0) {

				// 			locationCity.value = data.result.address_component.city;
				// 		} else {
				// 			console.log(data.message);
				// 		}

				// 	},
				// 	fail: function(res) {
				// 		console.log('地址解析失败：', res);
				// 	}
				// });
		locationParams.value.lat=res.latitude;
		locationParams.value.lng=res.longitude;
		wxLocation(locationParams.value).then((res) => {
				console.log("res=" + JSON.stringify(res))
				locationCity.value = res.address_component.city;
			})
			.catch((error) => {
				showToast(error.data.message || error.data.error);
			});
		
			},
			fail: function(res) {
				console.log('获取定位失败：', res);
			}
		});
	}
	// 切换设备
	let bindPickerChange = (e, storage) => {
		console.log("e", e)
		console.log("storage", storage)
		index.value = e.detail.value
		pickerData.value = storage[index.value] // 这里就是选中的对象
		console.log("pickerData", pickerData.value)
		let currentMerchantInfomation = pickerData.value
		// 存储当前设备信息
		store.setData({
			currentMerchantInfomation,
		});
		uni.setStorageSync("currentMerchantInfomation", currentMerchantInfomation);
	}


	// 查询设备列表
	function getEquipmentList() {
		// 报表查询条件
		let queryParams = {

			page: 1,
			size: 20,
			userId: store.userInfo.userId,
			oemUserNo: 'BU100002'
		};

		terminalQuery(queryParams).then((res) => {
			// console.log("res-res", res)
			let result = res.map(el => {
				return {
					merchantName: el.activityName,
					merchantId: el.sn,
					merchantNo: el.channelNo,
					lsMerchantNo: el.lsMerchantNo
				}
			})
			array.value = result
			console.log("result-result", JSON.stringify(array.value[0]))
			pickerData.value.merchantName = array.value[0].merchantName
			pickerData.value.merchantId = array.value[0].merchantId
			pickerData.value.merchantNo = array.value[0].merchantNo
			pickerData.value.lsMerchantNo = array.value[0].lsMerchantNo

			let currentMerchantInfomation = pickerData.value;
			console.log("result-result", JSON.stringify(pickerData.value))
			// 存储当前设备信息
			store.setData({
				currentMerchantInfomation,
			});
			uni.setStorageSync("currentMerchantInfomation", currentMerchantInfomation);
		});
	}
</script>
<style lang="scss" scoped>
	@import "./index.scss";
</style>